package com.bplow.deep.base.aspectj;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Aspect
@Service
public class ValidatorBeanAspectJ {
	
	@Autowired
	private Validator ivalidator;
	
	@Around("@annotation(com.bplow.deep.base.aspectj.ValidatorParam)")
    public Object processTx(ProceedingJoinPoint jp) throws java.lang.Throwable {
        
		Object argObj = jp.getArgs()[0];
		
		Set<ConstraintViolation<Object>> constraintViolations = ivalidator.validate(argObj);

		for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
			System.out.println("对象属性:" + constraintViolation.getPropertyPath());
			System.out.println("国际化key:" + constraintViolation.getMessageTemplate());
			System.out.println("错误信息:" + constraintViolation.getMessage());
			System.out.println(String.format(constraintViolation.getMessage(), constraintViolation.getPropertyPath()));
		}
		
        Object rvt = jp.proceed();
        
        return rvt;
    }
	
	@Around("@annotation(org.springframework.stereotype.Service)")
    public Object processTx2(ProceedingJoinPoint jp) throws java.lang.Throwable {
        System.out.println("AroundAdviceTest==执行目标方法之前，模拟开始事务 ...");
        // 执行目标方法，并保存目标方法执行后的返回值
        Object rvt = jp.proceed(new String[] { "被改变的参数" });
        System.out.println("AroundAdviceTest==执行目标方法之后，模拟结束事务 ...");
        return rvt;
    }


}
